<?php
declare (strict_types = 1);

namespace app\controller;
use app\Token as TokenServer;
use think\Request;
use think\facade\Db;
class User
{
	public function login(){
		$p = input('post.');
		$res = Db::query("select * from user_admin where name=?",[$p["name"]]);
		// return json($res);
		if($p["password"]==$res[0]["password"]){
			$token = (new TokenServer())->generateToken($res[0]["id"]);
			$r = ["code"=>200,"msg"=>"登陆成功","data"=>$token];
			return json($r);
		}else{
			return json(["code"=>500,"msg"=>"用户名或密码错误"]);
		}
	}
	public function info(Request $request){
		$uid = $request->uid;
		$data = [];
		$roleRes = Db::name("user_role_admin")->where("userId",$uid)->select();
		$data["role"] = Db::name("role_admin")->where("id",$roleRes[0]["roleId"])->select();
		$permissionRes = Db::name("role_permission_admin")->where("roleId",$roleRes[0]["roleId"])->select();
		$data["permission"] = Db::name("permission_admin")->where("id",$permissionRes[0]["permissionId"])->select();
		// $menuRes = Db::name("role_menu_admin")->where("roleId",$roleRes[0]["roleId"])->select();
		// $arr = "(";
		// foreach($menuRes as $key=>$item){
		// 	if($key==count($menuRes)-1){
		// 		$arr.$item.")";
		// 	}else{
		// 		$arr.$item.",";
		// 	}
		// }
		$data["menu"] = Db::query("select * from menu_admin where id in(select menuId from role_menu_admin where roleId=?)",[$roleRes[0]["roleId"]]);
		return json(["code"=>200,"data"=>$data]);
		// return json($uid);
	}
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
		$res = Db::query("select * from `user_admin`");
		return json(["code"=>200,"data"=>$res]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
		$p = input('post.');
	    $date = date('Y-m-d H:i:s',time());
		$data = ["name"=>$p["name"],"password"=>$p["password"],"createTime"=>$date,"updateTime"=>$date];
		Db::startTrans();
		try{
			$uid = Db::name("user_admin")->insertGetId($data);
			Db::execute("insert into user_role_admin (userId,roleId,createTime,updateTime) values (?,?,?,?)",[$uid,$p["role"],$date,$date]);
			Db::commit();
		}catch (\Exception $e) {
			  // 回滚事务
			Db::rollback();
		}
		return json(["code"=>200,"msg"=>"操作成功"]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update()
    {
        //
		$p = input("post.");
		$date = date('Y-m-d H:i:s',time());
		Db::startTrans();
		try{
			Db::name("user_admin")->where("id",$p["id"])->update(["name"=>$p["name"],"updateTime"=>$date]);
		    Db::name("user_role_admin")->where("userId",$p["id"])->update(["roleId"=>$p["role"]]);
			Db::commit();
		}catch(\Exception $e){
			Db::rollback();
		}
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {
        //
		$p = input("get.");
		Db::name("user_admin")->delete($p["id"]);
		Db::name("user_role_admin")->where("userId",$p["id"])->delete();
		return json(["code"=>200,"msg"=>"操作成功"]);
    }
}
